using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.VisualBasic;
using System;

public class AppHelper
{

    public struct Margins
    {
        public Margins(Thickness t)
        {
            Left = System.Convert.ToInt32(Conversion.Fix(t.Left));
            Right = System.Convert.ToInt32(Conversion.Fix(t.Right));
            Top = System.Convert.ToInt32(Conversion.Fix(t.Top));
            Bottom = System.Convert.ToInt32(Conversion.Fix(t.Bottom));
        }
        public int Left;
        public int Right;
        public int Top;
        public int Bottom;
    }
 

    //[System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)]
    static public void DwmExtendFrameIntoClientArea(IntPtr hwnd, Margins pMargins)
    {
    }

    // [System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)]
    static public bool DwmIsCompositionEnabled()
    {
        return true;
    }

    public static bool IsGlassEnabled
    {
        get
        {
            return DwmIsCompositionEnabled();
        }
    }

    public static bool ExtendGlassFrame(Window window, Thickness margin)
    {
        if (!DwmIsCompositionEnabled())
        {
            return false;
        }
        var hwnd = new WindowInteropHelper(window).Handle;
        var margins = new Margins(margin);
        var background = new SolidColorBrush(Colors.Red);
        if (hwnd == IntPtr.Zero)
        {
            throw (new InvalidOperationException("The Window must be shown before extending glass."));
        }
        background.Opacity = 0.5;
        window.Background = Brushes.Transparent;
        // ReSharper disable PossibleNullReferenceException
        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
        // ReSharper restore PossibleNullReferenceException
        DwmExtendFrameIntoClientArea(hwnd, margins);
        return true;
    }
}

